package com.wjt.homestayms.controller;

import com.wjt.homestayms.common.Result;
import com.wjt.homestayms.pojo.Orderinfo;
import com.wjt.homestayms.common.PageRequest;
import com.wjt.homestayms.common.PageResult;
import com.wjt.homestayms.service.IOrderinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.*;

@RestController
public class OrderinfoController {

    @Autowired
    IOrderinfoService orderinfoService;


    @GetMapping("/getOrderInfoList")
    public Map<String, Object> getOrderInfoList(PageRequest pageRequest, Integer id,Integer rNum,String tName) {
        Map<String, Object> map = new HashMap<>();
        PageResult resultPage = null;
        if (id != null) {
            resultPage = orderinfoService.listAll(pageRequest, id);
        }else if(rNum != null){
            resultPage = orderinfoService.listAllbyRnum(pageRequest, rNum);
        }else if(tName != null){
            resultPage = orderinfoService.listAllbyTname(pageRequest, tName);
        }
        return Result.queryResult(map,resultPage);
    }


    /**
     * 根据主键查询
     * @param oId 主键
     * @return 返回记录，没有返回null
     */
    @RequestMapping("/getOrderById")
    public Result<Orderinfo>  getOrderById(Integer oId) {
        return orderinfoService.getById(oId);
    }

    /**
     * 新增，忽略null字段
     * @param orderinfo 新增的记录  (rNum,price,tName,id,time,deposit,day)
     * @return 返回影响行数
     */
    @RequestMapping("/addOrder")
    public Map<String, Object> addOrder(Orderinfo orderinfo) {

        orderinfo.setRefund(null);
        Map<String, Object> map = new HashMap<>();
        if (orderinfoService.insertIgnoreNull(orderinfo) > 0) {
            map.put("code", 200);
            map.put("status", true);
        } else {
            map.put("code", 500);
            map.put("status", false);
        }
        return map;
    }

    /**
     * 修改，忽略null字段
     *
     * @param orderinfo 修改的记录 (oId,rNum,price,tName,time,deposit,day)
     * @return 返回影响行数
     */
    @RequestMapping("/updateOrder")
    public Map<String, Object> updateOrder(Orderinfo orderinfo) {
        Map<String, Object> map = new HashMap<>();
        orderinfo.setRefund(null);
        if (orderinfoService.updateIgnoreNull(orderinfo) > 0) {
            map.put("code", 200);
            map.put("status", true);
        } else {
            map.put("code", 500);
            map.put("status", false);
        }
        return map;
    }

    /**
     * 删除记录
     * @param oId 待删除的记录
     * @return 返回影响行数
     */
    @RequestMapping("/deleteOrder")
    public Map<String, Object> deleteOrder(Integer oId) {
        Map<String, Object> map = new HashMap<>();
        if (orderinfoService.delete(oId) > 0) {
            map.put("code", 200);
            map.put("status", true);
        } else {
            map.put("code", 500);
            map.put("status", false);
        }
        return map;
    }

    @GetMapping("/getInCount")
    @ResponseBody
    public Result getInCount(Integer id){
        List<Orderinfo> orderinfos = orderinfoService.getOrderinfos(id);
        Iterator<Orderinfo> iterator = orderinfos.iterator();
        Integer[] data = {0,0,0,0,0,0};
        while (iterator.hasNext()){
            Orderinfo orderinfo = iterator.next();
            LocalDate time = orderinfo.getTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            String month = time.getMonth().toString();
            if(month.equals("JULY")){
                data[0] = data[0] + orderinfo.getPrice();
            }else if(month.equals("AUGUST")){
                data[1] = data[1] + orderinfo.getPrice();
            }else if(month.equals("SEPTEMBER")){
                data[2] = data[2] + orderinfo.getPrice();
            }else if(month.equals("OCTOBER")){
                data[3] = data[3] + orderinfo.getPrice();
            }else if(month.equals("NOVEMBER")){
                data[4] = data[4] + orderinfo.getPrice();
            }else if(month.equals("DECEMBER")){
                data[5] = data[5] + orderinfo.getPrice();
            }
        }
        Map<String,Integer> map = new HashMap<>();
        map.put("qi",data[0]);
        map.put("ba",data[1]);
        map.put("jiu",data[2]);
        map.put("shi",data[3]);
        map.put("shiyi",data[4]);
        map.put("shier",data[5]);
        return Result.ok(map);
    }
}
